package com.itheima.Reggie.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Reggie.Entity.Category;
import com.itheima.Reggie.Entity.Dish;
import com.itheima.Reggie.Entity.Setmeal;
import com.itheima.Reggie.Mapper.CategoryMapper;
import com.itheima.Reggie.Service.CategoryService;
import com.itheima.Reggie.Service.DishService;
import com.itheima.Reggie.Service.SetmealService;
import com.itheima.Reggie.common.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("当前菜品分类下关联了菜品,不可以删除...");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("当前套餐分类下关联了套餐,不可以删除...");
        }
        super.removeById(id);
    }
}
